﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter05
{
    public class Code02
    {
        public static void Begin()
        {
            {
                route myRoute;
                myRoute.direction = orientation.north;
                myRoute.distance = 2.5;
            }
            {
                string friednName1 = "Robert Barwell";
                string friednName2 = "Mike Parry";
                string friednName3 = "Jeremy Beacock";
                Console.WriteLine(string.Format("\n", friednName1, friednName2, friednName3));
            }
            {
                string[] friendNames = new string[3] { "Robert Barwell", "Mike Parry", "Jeremy Beacock" };
                int i;
                for (i = 0; i < 3; i++)
                {
                    Console.WriteLine("Name with is index of {0}: {1}", i, friendNames[i]);
                }
                Console.ReadLine();
            }
            {
                string[] friendNames = new string[3] { "Robert Barwell", "Mike Parry", "Jeremy Beacock" };
                Console.WriteLine("Here are {0} of my friends:", friendNames.Length);
                foreach (string friendname in friendNames)
                {
                    Console.WriteLine(friendname);
                }
                Console.ReadLine();
            }
            {
                double[,] hillHeight = new double[3, 4] { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };
                foreach (double height in hillHeight)
                {
                    Console.WriteLine("{0}", height);
                }
                Console.ReadLine();
            }
            {
                int[][] jaqggedIntArray = new int[3][] { new int[3] { 1, 2, 3 }, new int[1] { 1 }, new int[2] { 1, 2 } };
                int[][] divisors1To10 = new int[10][]
                {
                    new int[1]{1},
                    new int[2]{1,2},
                    new int[2]{1,3},
                    new int[3]{1,2,4},
                    new int[2]{1,5},
                    new int[4]{1,2,3,6},
                    new int[2]{1,7},
                    new int[4]{1,2,4,8},
                    new int[3]{1,3,9},
                    new int[4]{1,2,5,10},
                };
                foreach (int[] divisorsOfInt in divisors1To10)
                {
                    foreach (int divisor in divisorsOfInt)
                    {
                        Console.Write(divisor + " ");
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        }

        public static void BeginEx02()
        {
            orientation myDirection = orientation.north;
            Console.WriteLine("myDirection={0}", myDirection);
            Console.ReadLine();

            byte directionByte;
            String directionString;
            myDirection = orientation.north;
            Console.WriteLine("myDirection = {0}", myDirection);
            directionByte = (byte)myDirection;
            //枚举转换为字符串方式1
            directionString = Convert.ToString(myDirection);
            Console.WriteLine("string equivalent = {0}", directionString);
            //枚举转换为字符串方式2
            directionString = myDirection.ToString();
            Console.WriteLine("string equivalent = {0}", directionString);
            //字符串转换为枚举
            string myString = "north";
            myDirection = (orientation)Enum.Parse(typeof(orientation), myString);
            Console.WriteLine("myDirection = {0}", myDirection);
            Console.ReadLine();
            Console.WriteLine("byte equivalent = {0}", directionByte);
            Console.WriteLine("string equivalent = {0}", directionString);
            Console.ReadLine();
        }

        public static void BeginEx03()
        {
            route myRoute;
            int myInt = -1;
            double myDistance;
            Console.WriteLine("1) North\n2) South\n3) East\n4) West");
            do
            {
                Console.WriteLine("Select a direction:");
                try
                {
                    myInt = Convert.ToInt32(Console.ReadLine());
                }
                catch (FormatException ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(myInt);
                }
            }
            while ((myInt < 1) || (myInt > 4));
            Console.WriteLine("Input a distance:");
            myDistance = Convert.ToDouble(Console.ReadLine());
            myRoute.direction = (orientation)myInt;
            myRoute.distance = myDistance;
            Console.WriteLine("myRoute specifies a direction of {0} and distance of {1}", myRoute.direction, myRoute.distance);
            Console.ReadLine();
        }

        public static void BeginEx04()
        {
            string[] friendNames = new string[3] { "Robert Barwell", "Mike Parry", "Jeremy Beacock" };
            int i = 0;
            Console.WriteLine("Here are {0} of my friends:", friendNames.Length);
            for (i = 0; i < friendNames.Length; i++)
            {
                Console.WriteLine(friendNames[i]);
            }
            Console.ReadLine();
        }
    }

    public enum orientation : byte
    {
        north = 1,
        south = 2,
        east = 3,
        west = 4,
    }

    public struct route
    {
        public orientation direction;
        public double distance;
    }
}
